/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


var common = null;

Array.prototype.remove = function (val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};

$(function () {
    common = {
        ajax: function (url, data, cb) {
            $.ajax({
                url: url,
                type: "post",
                dataType: "json",
                data: data,
                success: cb
            });
        },
        getHtml: function (url, data, cb) {
            $.ajax({
                url: url,
                type: "get",
                dataType: "html",
                data: data,
                success: cb
            });
        },
        htmlAjax: function (url, data, cb) {
            $.ajax({
                url: url,
                type: "post",
                dataType: "html",
                data: data,
                success: cb
            });
        },
        redirect: function (url) {
            window.location.href = url;
        },
        reload: function () {
            window.location.reload();
        },
        alert: function (title, content, sure) {
            $("#alertModal").find(".modal-title").html(title);
            $("#alertModal").find(".modal-body").html(content);
            $("#alertModal").modal('toggle');

            if (typeof (sure) == 'function') {
            	$('#alertModalBtn').unbind("click"); 
                $("#alertModalBtn").click(sure);
            }
        },
        confirm: function (title, content, sure, cancel) {
            $("#confirmModal").find(".modal-title").html(title);
            $("#confirmModal").find(".modal-body").html(content);
            $("#confirmModal").modal('toggle');
            if (typeof (cancel) === 'function') {
            	$('#confirmModalCancel').unbind("click"); 
                $("#confirmModalCancel").click(cancel);
            }
            if (typeof (sure) === 'function') {
            	$('#confirmModalBtn').unbind("click"); 
                $("#confirmModalBtn").click(sure);
            }

        },
        loading: function (content) {
            if (content) {
                $("#loadModal").find(".loading-title").html(content);
            }
            $("#loadModal").modal({show: true, backdrop: 'static', keyboard: false});

        },
        closeLoading: function () {
            $("#loadModal").modal('hide');
        },
        inArray: function (value, array) {
            var status = false;
            for (var key in array) {
                if (array[key] == value) {
                    status = true;
                }
            }
            return status;
        },
        isEmptyArray: function (obj) {
            if (JSON.stringify(obj) === '[]') {
                return true;
            }
            return false;
        },
        showTip: function(id, msg) {
            var target = $("#"+id);
            target.attr("data-original-title", msg);
            $('[data-toggle="tooltip"]').tooltip();
            target.tooltip('show');
            target.focus();
            //2秒后消失提示框
            var id = setTimeout(
                function () {
                    target.attr("data-original-title", "");
                    target.tooltip('hide');
                }, 3000
            );
        },
        isJsonString: function(str) {
            try {
                if (typeof JSON.parse(str) == "object") {
                    return true;
                }
            } catch(e) {
            }
            return false;
        },
        modifyPwd: function (url) {
            common.htmlAjax(url, {}, function (html) {
                $("#commonModal").html(html).modal({show: true, backdrop: 'static', keyboard: false});
            })
        },
        /**
         * 格式化数字
         * @param value:数字
         * @param numberOfDecimal:小数位数
         * @param thousenSeparator:千位分隔符,默认为英文逗号
         * @returns
         */
        formatNumber: function (value, numberOfDecimal, thousenSeparator){
            if(typeof(numberOfDecimal) == "undefined"){
                numberOfDecimal = 0;
            }
            if(typeof(thousenSeparator) == "undefined"){
                thousenSeparator = ",";
            }
            value = parseFloat(value);
            value = value.toFixed(numberOfDecimal);
            var val_string = value+'';
            var tmp = val_string.split('.');
            var abs_val_string = (tmp.length == 2) ? tmp[0] : val_string;
            var deci_string = ('0.' + (tmp.length == 2 ? tmp[1] : 0)).substr(2);
            var nb = abs_val_string.length;

            for (var i = 1 ; i < 4; i++){
                if (value >= Math.pow(10, (3 * i))){
                    abs_val_string = abs_val_string.substring(0, nb - (3 * i)) + thousenSeparator + abs_val_string.substring(nb - (3 * i));
                }
            }
            if (parseInt(numberOfDecimal) == 0){
                return abs_val_string;
            }
            return abs_val_string + "." + (deci_string > 0 ? deci_string : '00');
        },
        loadTableData: function (tableId,url,columns,queryParams,option,cbSuccess) {
            $("#"+tableId).bootstrapTable('destroy');
            var options = {
                url: url,
                method: "get",
                dataType: "json",
                locale: "zh-CN",
                cache: false, // 设置为 false 禁用 AJAX 数据缓存， 默认为true
                striped: true,  //表格显示条纹，默认为false
                pageList: [], // 设置页面可以显示的数据条数
                pageNumber: 1, // 首页页码
                sidePagination: 'server', // 设置为服务器端分页
                queryParams: function (params) { // 请求服务器数据时发送的参数，可以在这里添加额外的查询参数，返回false则终止请求
                    var queryParam = {
                        pageSize: params.limit // 每页要显示的数据条数
                    };
                    if(option.pageSize){
                        // 每页大小
                        queryParam.p = parseInt(params.offset/option.pageSize)+1; // 当前页
                    }
                    for(var attr in queryParams){
                        queryParam[attr] = queryParams[attr];
                    }
                    console.log(queryParam);
                    return queryParam;
                },
                columns: columns,
                onLoadSuccess: function(){  //加载成功时执行
                    $(".pagination-detail").remove();
                    cbSuccess && cbSuccess();
                },
                onLoadError: function(){  //加载失败时执行
                    console.info("加载数据失败");
                }
            };
            if(option.height){
                // 表格高度
                options.height = option.height;
            }
            if(option.pagination){
                // 在表格底部显示分页组件，默认false
                options.pagination = option.pagination;
            }
            if(option.pageSize){
                // 每页大小
                options.pageSize = option.pageSize;
            }
            $("#"+tableId).bootstrapTable(options);
        }
    };
});
if(typeof(webRoot) != "undefined") {
    document.write('<script type="text/javascript" src="'+webRoot+'/Public/js/layer/layer.js"></script>');
}

/**
 * 后台左侧导航JS
 * @returns {undefined}
 */
$(function () {
    var Accordion = function (el, multiple) {
        this.el = el || {};
        this.multiple = multiple || false;

        // Variables privadas
        var links = this.el.find('.link');
        // Evento
        links.on('click', {el: this.el, multiple: this.multiple}, this.dropdown)
    };

    Accordion.prototype.dropdown = function (e) {
        var $el = e.data.el;
        $this = $(this),
                $next = $this.next();

        $next.slideToggle();
        $this.parent().toggleClass('open');

        if (!e.data.multiple) {
            $el.find('.submenu').not($next).slideUp().parent().removeClass('open');
        }
        ;
    };

    var accordion = new Accordion($('#accordion'), false);
    if(typeof(controllerName) == "undefined"){
        return false;
    }
    $('#accordion').find("li[thisC='"+controllerName+"']").addClass("open");
    $('#accordion').find("li[thisC='"+controllerName+"']").children(".submenu").show();
    $('#accordion').find("li[thiscf='"+controllerName+"_"+actionName+"']").addClass("submenuOpen");

    //分页form提交
    if($(".page").length) {
        $(".page").find("a").click(function () {
            var url = $(this).attr("href");
            if($(".searchForm").length) {
                if(url != "#") {
                    $(".searchForm").attr("action",url).submit();
                    $(this).attr("href","javascript:void(0);");
                }
            }
        });
    }

    function getNotice() {
        if (!hasNotice()) {
            return ;
        }
        layer.open({
            type: 2,
            title: '系统通知',
            closeBtn: 1, //显示关闭按钮
            shade: false,
            shadeClose: false,
            area: ['360px', '230px'],
            skin: 'layui-layer-lan', //加上边框
            offset: 'rb', //右下角弹出
            time: 10000, //10秒后自动关闭
            anim: 2,
            content: [webRoot+'/index.php/Admin/Space/Notice/latest', 'no'], //iframe的url，no代表不显示滚动条
            success: function(layero, index){

            }
        });
    }
    
    function hasNotice() {
        var showWin = false;
        $.ajax({
            url: webRoot+"/index.php/Admin/Space/Notice/checkNotice",
            type: "post",
            dataType: "json",
            async: false,
            data: {},
            success: function (cbData) {
                if (cbData > 0) {
                    showWin = true;
                }
            }
        });
        return showWin;
    }

    if(typeof(webRoot) != "undefined") {
        //getNotice();
        //定时获取系统通知
        //setInterval(getNotice,30000);
    }
});